/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

package cn.knet.sp.ncrm.modules.sys.producttype.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.knet.sp.ncrm.common.config.Global;
import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.sys.producttype.entity.ProductType;
import cn.knet.sp.ncrm.modules.sys.producttype.service.ProductTypeService;
import cn.knet.sp.ncrm.modules.sys.user.entity.User;
import cn.knet.sp.ncrm.modules.sys.utils.ProductUtils;
import cn.knet.sp.ncrm.modules.sys.utils.UserUtils;

/**
 * 产品类型crudController
 * @author zhangbin
 * @version 2015-08-24
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/productType")
public class ProductTypeController extends BaseController {

	@Autowired
	private ProductTypeService productTypeService;
	
	@ModelAttribute
	public ProductType get(@RequestParam(required=false) String id) {
		ProductType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = productTypeService.get(id);
		}
		if (entity == null){
			entity = new ProductType();
		}
		return entity;
	}

	/**
	 * 去除前后空格
	 * @author zhangbin
	 * @date 2015 /08/26 18:15:06
	 * void
	 * @param productType the product type
	 */
	private void trim(ProductType productType) {
		if(productType!=null){
			if(StringUtils.isNotBlank(productType.getProductTypeName())){
				productType.setProductTypeName(StringUtils.trim(productType.getProductTypeName()));
			}
			if(StringUtils.isNotBlank(productType.getProductTypeValue())){
				productType.setProductTypeValue(StringUtils.trim(productType.getProductTypeValue()));
			}
		}
	}
	
	@RequiresPermissions("sys:productType:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProductType productType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProductType> page = productTypeService.findPage(new Page<ProductType>(request, response), productType); 
		model.addAttribute("page", page);
		return "modules/sys/product/productTypeAjaxList";
	}

	@RequiresPermissions("sys:productType:view")
	@RequestMapping(value = "listAjaxData")
	@ResponseBody
	public Page listAjaxData(ProductType productType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProductType> page = productTypeService.findPage(new Page<ProductType>(request, response), productType);
		return page;
	}

	@RequiresPermissions("sys:productType:view")
	@RequestMapping(value = "form")
	public String form(ProductType productType, Model model) {
		productType.addHistoryFlag();
		model.addAttribute("productType", productType);
		return "modules/sys/product/productTypeForm";
	}

	@RequiresPermissions("sys:productType:edit")
	@RequestMapping(value = "save")
	public String save(ProductType productType, Model model, RedirectAttributes redirectAttributes) {
		this.trim(productType); //记得trim
		if (!beanValidator(model, productType)){
			return form(productType, model);
		}
		if("WU".equals(productType.getProductTypeValue())){
			addMessage(model, "简称不能为WU!");
			return form(productType, model);
		}
		ProductType same = new ProductType();
		same.setProductTypeName(productType.getProductTypeName());
		same.setProductTypeValue(productType.getProductTypeValue());
		same.setOldTypeid(productType.getOldTypeid());
		same.setId(productType.getId());
		int count = productTypeService.findDistinctProductType(same); //先查询产品是否存在
		if(count>0){
			addMessage(model, "当前记录已经存在,请确认 产品名称、类型标签、旧类型ID是唯一的");
			return form(productType, model);
		}
		if(productType.getIsNewRecord()){ //若为创建
			productType.setDateCreated(new java.util.Date());
		}
		productType.setLastUpdated(new java.util.Date());// 创建和更新都修改更新时间
		productTypeService.save(productType);
		ProductUtils.clearCache(productType);
		addMessage(redirectAttributes, "保存产品类型成功");
		return "redirect:"+Global.getAdminPath()+"/sys/productType/?repage";
	}



	@RequiresPermissions("sys:productType:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(ProductType productType) {
		AjaxMessage result = AjaxMessage.success();
		User user = UserUtils.getUser();
		if(user==null||!user.isAdmin()){
			result.makeError("Gandalf you have not power~~~");
			return result;
		}
		try {
			if(productType!=null){
				productTypeService.delete(productType);
				ProductUtils.clearCache(productType);
				result.appendMessage("删除产品类型成功!");
			}else{
				result.makeError("产品类型不存在,删除失败!");
			}
		} catch (Exception e) {
			logger.warn("删除产品失败", e);
			result.makeError("删除失败!系统发生异常");
		}
		return result;
	}

	@RequiresPermissions("sys:productType:edit")
	@RequestMapping(value = "updateStatus")
	@ResponseBody
	public Object updateStatus(@RequestParam(value = "ids[]") String[] ids,String status) {
		AjaxMessage result = AjaxMessage.success();
		try {
			productTypeService.updateStatus(ids,status);
			ProductUtils.clearCache(); //缓存全部清除
			result.setMessage("批量更新状态成功");
		} catch (Exception e) {
			logger.warn("更新产品状态失败", e);
			result.makeError("更新产品状态失败!系统发生异常");
		}
		return result;
	}

}